<?php

class ServiceWordFilter {
	/**
	 * 检查有否敏感词
	 * 		
	 * @param unknown_type $str
	 * //@param word_lib 需过滤词库列表 暂时先用bbs词库吧
	 * @param word_lib 过滤词库编号,默认为2
	 * @param unknown_type $encoding
	 * @return 有敏感词 返回true，无敏感词 返回false
	 */
	//static function ifHasBad( $str,$word_lib = array(),$encoding = 'utf-8') {
	static function ifHasBad( $str,$word_lib = 2,$encoding = 'utf-8') {
		
		$encoding = strtoupper($encoding);
		
		//$config = C('WORD_FILTER');
		$config=array(
			'hosts' => array(
				//'s1' => array('host' => '10.245.75.74','port' => '5678'),
				's1' => array('host' => '192.168.117.100','port' => '5678'),
			),
		);
		
		//$url = 'http://'.$config['hosts']['s1']['host'].':'.$config['hosts']['s1']['port'].'/dirty/bbs';
		$url = 'http://'.$config['hosts']['s1']['host'].':'.$config['hosts']['s1']['port'].'/dirty/'.$word_lib;
		
		if ($encoding != 'GBK' && in_array($encoding,array('UTF-8'))) {
			$str = iconv($encoding,'GBK//IGNORE',$str);
		}


		$process = curl_init();
		curl_setopt( $process, CURLOPT_URL, $url );
		
		$headers = array(
					'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg',
					'Connection: Keep-Alive',
					'Content-type: application/x-www-form-urlencoded;charset=GBK'
					//'Content-type: application/x-www-form-urlencoded;charset=UTF-8'
				);

		curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
		
		curl_setopt($process, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
		
		curl_setopt($process, CURLOPT_TIMEOUT, 30);
		
		curl_setopt($process, CURLOPT_POSTFIELDS, $str);
		
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($process, CURLOPT_POST, 1);
		
		//curl_setopt($process, CURLOPT_HEADER, 1);
		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
		
		
		$ret = curl_exec($process);
		curl_close($process);
		//return $ret?true:false;
		if($ret!=false){
			if ($encoding != 'GBK' && in_array($encoding,array('UTF-8'))) {
				$ret = iconv('GBK//IGNORE',$encoding,$ret);
			}
		}
		
		return $ret;
		
	}
}

/*
// Exmaple
//$ret=ServiceWordFilter::ifHasBad("gcdgcdgcd");
//$ret=ServiceWordFilter::ifHasBad("guomingdangguomingdang");
//$ret=ServiceWordFilter::ifHasBad("胡锦涛");
$ret=ServiceWordFilter::ifHasBad("温家宝温家宝");
//$ret=ServiceWordFilter::ifHasBad("国务院",1);
printf("%s\n",$ret);


$ret = ServiceWordFilter::ifHasBad("温家宝国务院温家宝", 2);
printf("%s",$ret);

$ret = ServiceWordFilter::ifHasBad("何全fuck全国", 2);
printf("%s",$ret);
*/
